import requests
from lxml import etree
import time

headers = {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "accept-language": "zh-CN,zh;q=0.9",
    "cache-control": "max-age=0",
    "if-none-match": "W/\"1dx7hkymbjocrj\"",
    "priority": "u=0, i",
    "referer": "https://www.xinpianchang.com/discover/article-27-180",
    "sec-ch-ua": "\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\", \"Google Chrome\";v=\"138\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "document",
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "same-origin",
    "sec-fetch-user": "?1",
    "upgrade-insecure-requests": "1",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36"
}
cookies = {
    "FECWS": "254908952d7a40a2707ccf547c2a004bc463ed14b11fd159c0ce2f486899fc1eb02d5e923f62da308cb683a65cbc8d48386af54548ec1d7ddb0d54042bd755edfb66609c2963707921af066f3886b21441",
    "Device_ID": "34m0k1f3pvmdebzwmv",
    "Authorization": "C96DB9545448684D254486419854486BCCB544862528A0544D98",
    "sensorsdata2015jssdkcross": "%7B%22distinct_id%22%3A%22198317607771ae1-0776b1b0a2f0e6-26011151-1821369-198317607781bdf%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22%24device_id%22%3A%22198317607771ae1-0776b1b0a2f0e6-26011151-1821369-198317607781bdf%22%7D",
    "Hm_lvt_446567e1546b322b726d54ed9b5ad346": "1753176459,1753232168,1753237398",
    "HMACCOUNT": "F88B9A85A017A738",
    "default_home_tab": "editor_recommend",
    "lastHomeTab": "editor_recommend",
    "Hm_lpvt_446567e1546b322b726d54ed9b5ad346": "1753242337",
    "FECAS": "1Dgt/iT9V+RlekvsotGkuhY8lGEhbl5IKwT1wIP9B/9G/63zCI2C+cWGf3rK1vzokQ+/hIE7yNfOa+4tXbATvrRe+nKWggoD9FjXtv+T+CRb/3Cm/aDIwv+A8VIXhL7MrQIfRuBbmAilZvFI7vvMukON+aahPI159he4qV08khSjscG+0kgng45GznjInVn4md"
}
for page in range(1, 8):
    if page == 1:
        url = "https://www.xinpianchang.com/discover/article-27-180"
    else:
        url = f"https://www.xinpianchang.com/discover/article-27-180-all-all-0-0-score-pp{page}"
    print(url)
    response = requests.get(url, headers=headers, cookies=cookies)
    print(response.status_code)
    # print(response.text)
    # print(response)
    html_str = response.content.decode()
    # print(html_str)
    root = etree.HTML(html_str)

    sift_list = root.xpath('//div[@class="sc-c1bad5f3-0 iMhhFo"]')

    for item in sift_list:
        title = "".join(
            item.xpath(".//a[@class='flex-1']/h2[@class='line-clamp-1 break-all']/text()"))
        categories = "|".join(item.xpath(".//div[@class='flex items-center truncate']/div[@class='truncate']/text()"))
        link = "".join(item.xpath(".//a[@class='flex-1']/@href"))
        print(title, categories, link)
    time.sleep(5)
    break
